require(tensorEVD)
# Generate rectangular some covariance matrices
n = 30; p = 10
K = crossprod(matrix(rnorm(n*p), ncol=n)) # n x n matrix
Sigma = crossprod(matrix(rnorm(n*p), ncol=p)) # p x p matrix
Theta = crossprod(matrix(rnorm(n*p), ncol=p)) # p x p matrix
# ==============================================
# Kronecker covariance
# ==============================================
G1 = Kronecker_cov(Sigma, K, Theta = Theta)
# it must equal to:
D = diag(n) # diagonal matrix of dimension n
G2 = Kronecker(Sigma, K) + Kronecker(Theta, D)
all.equal(G1,G2)
# (b) Swapping the order of the matrices
G1 = Kronecker_cov(Sigma, K, Theta, swap = TRUE)
# in this case the kronecker is swapped:
G2 = Kronecker(K, Sigma) + Kronecker(D, Theta)
all.equal(G1,G2)
# \donttest{
# (c) Selecting specific entries of the output
# We want only some rows and columns
rows = c(1,3,5)
cols = c(10,30,50)
G1 = Kronecker_cov(Sigma, K, Theta, rows=rows, cols=cols)
# this can be preferable instead of:
G2 = (Kronecker(Sigma, K) + Kronecker(Theta, D))[rows,cols]
all.equal(G1,G2)
# (d) Inplace calculation
# overwrite the output at the same address as the input:
G1 = K[] # copy of K to be used as input
add = pryr::address(G1) # address of G on entry
G1 = Kronecker_cov(Sigma=0.5, G1, Theta=1.5)
pryr::address(G1) == add # on exit, G was moved to a different address
G2 = K[]
add = pryr::address(G2)
G2 = Kronecker_cov(Sigma=0.5, G2, Theta=1.5, inplace=TRUE)
pryr::address(G2) == add # on exit, G remains at the same address
all.equal(G1,G2)
# }
# ==============================================
# Hadamard covariance
# ==============================================
# Define IDs for a Hadamard of size m x m
m = 1000
IDS = sample(1:p, m, replace=TRUE)
IDK = sample(1:n, m, replace=TRUE)
G1 = Hadamard_cov(Sigma, K, Theta, IDS=IDS, IDK=IDK)
# it must equal to:
G2 = Sigma[IDS,IDS]*K[IDK,IDK] + Theta[IDS,IDS]*D[IDK,IDK]
all.equal(G1,G2)
# \donttest{
# (b) Inplace calculation
# overwrite the output at the same address as the input:
G1 = K[] # copy of K to be used as input
add = pryr::address(G1) # address of G on entry
G1 = Hadamard_cov(Sigma=0.5, G1, Theta=1.5, IDS=rep(1,n))
pryr::address(G1) == add # on exit, G was moved to a different address
G2 = K[]
add = pryr::address(G2)
G2 = Hadamard_cov(Sigma=0.5, G2, Theta=1.5, IDS=rep(1,n), inplace=TRUE)
pryr::address(G2) == add # on exit, G remains at the same address
all.equal(G1,G2)
# }
Run the code above in your browser using DataLab